package com.oo.app.ui.main.activity.boy;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.gelitenight.waveview.library.WaveView;
import com.oo.app.R;
import com.oo.app.base.activities.BaseActivity;
import com.oo.app.ui.im.activity.MessageActivity;
import com.oo.app.ui.main.activity.MyPageActivity;
import com.oo.app.ui.main.adapter.GuidePagerAdapter;
import com.oo.app.utils.WaveHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @创建者 喻亚龙
 * @描述
 */

public class HomeBoyActivity extends BaseActivity implements ViewPager.OnPageChangeListener ,View.OnClickListener{

    @BindView(R.id.first_viewpager)
    ViewPager mViewPager;

    @BindView(R.id.viewpager_tag_viewgroup)
    ViewGroup mViewGroup;

    @BindView(R.id.wave)
    WaveView waveView;

    @BindView(R.id.iv_gif)
    ImageView ivGif;

    @BindView(R.id.tv_mes)
    TextView tvMes;

    //实例化圆点View
    private ImageView mImageView;
    private ImageView[] mImageViewArray;
    //图片资源的数组
    private int[] mImageIdArray;
    //图片的集合
    private List<View> mViewList;

    private WaveHelper mWaveHelper;

    boolean isMes = true;

    private int mBorderColor = Color.parseColor("#FFFFFF");

    public static void show(Context context) {
        Intent intent = new Intent(context, HomeBoyActivity.class);
        context.startActivity(intent);
    }


    @Override
    protected int getContentView() {
        return R.layout.activity_home_boy;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        initView();

        mWaveHelper = new WaveHelper(waveView);
        waveView.setBorder(0, getResources().getColor(R.color.white));

        waveView.setWaveColor(
                Color.parseColor("#88FFFFFF"),
                Color.parseColor("#FFFFFF"));
        mBorderColor = Color.parseColor("#FFFFFF");
        waveView.setBorder(0, mBorderColor);

        waveView.setShapeType(WaveView.ShapeType.SQUARE);

        RequestOptions options = new RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        Glide.with(HomeBoyActivity.this).load(R.mipmap.home_male_btn_chat_animation)
                .apply(options).into(ivGif);

        initAnimation();
        //加载ViewPager
        initViewpager();
        //加载底部圆点
        initViewPagerTag();

    }


    public void initAnimation() {
        // 从屏幕底部进入的动画
        TranslateAnimation animation = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f
        );
        animation.setDuration(100);
        tvMes.startAnimation(animation);

        mHandler.postDelayed(rMes, 2000);//延时100毫秒

    }


    Handler mHandler = new Handler();
    Runnable rMes = new Runnable() {

        @Override
        public void run() {
            //判断是否已经显示出来，显示出来了就淡出，未显示就平移弹出
            if (isMes) {
                //do something
                AnimationSet aset = new AnimationSet(true);
                AlphaAnimation aa = new AlphaAnimation(1, 0);
                aa.setDuration(2000);
                aset.addAnimation(aa);
                tvMes.startAnimation(aset);
                isMes = false;
                mHandler.postDelayed(this, 2000);
            } else {
                isMes = true;
                initAnimation();
            }
        }
    };

    private void initView() {
//        banner.bannerPlay(2000);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mWaveHelper.cancel();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mWaveHelper.start();
    }

    @OnClick({R.id.iv_user,R.id.iv_message,R.id.iv_gif})
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_user:
                MyPageActivity.show(HomeBoyActivity.this);
                break;
            case R.id.iv_message:
                MessageActivity.show(HomeBoyActivity.this);
                break;
            case R.id.iv_gif:

                break;
        }
    }

    /**
     * 加载底部圆点
     */
    private void initViewPagerTag() {
        //根据ViewPager的item数量实例化数组
        mImageViewArray = new ImageView[mViewList.size()];
        //循环新建底部圆点imageview，将生成的imageview保存到数组中
        for (int i = 0; i < mViewList.size(); i++) {
            mImageView = new ImageView(this);
            mImageView.setLayoutParams(new ViewGroup.LayoutParams(30, 30));
            //设置小原点间距
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(30, 30);
            params.setMargins(20, 0, 0, 0);
            mImageView.setLayoutParams(params);
            mImageView.setPadding(20, 0, 20, 0);
            mImageViewArray[i] = mImageView;
            //第一个页面需要设为选中状态，这里要使用两张不同的图片（选中与未选中）
            if (i == 0) {
                mImageView.setBackgroundResource(R.mipmap.checkbox_checked);
            } else {
                mImageView.setBackgroundResource(R.mipmap.ic_launcher);
            }
            //将数组中的imageview加入到viewgroup
            mViewGroup.addView(mImageViewArray[i]);
        }
    }

    /**
     * 加载ViewPager
     */
    private void initViewpager() {
        //实例化图片资源
        mImageIdArray = new int[]{R.mipmap.banner1, R.mipmap.banner2};

        mViewList = new ArrayList<View>();
        //获取一个layout参数，设置为match_parent
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT
                , LinearLayout.LayoutParams.MATCH_PARENT);

        //循环创建view并进入集合
        for (int i = 0; i < mImageIdArray.length; i++) {
            //new imageview并设置全屏和图片资源
            ImageView imageView = new ImageView(this);
            imageView.setLayoutParams(params);
            imageView.setBackgroundResource(mImageIdArray[i]);

            //将imageview加入到View集合中
            mViewList.add(imageView);
        }
        //View集合数据初始化好，setAdapter就可以了
        mViewPager.setAdapter(new GuidePagerAdapter(HomeBoyActivity.this, mViewList));
        //添加ViewPager的滑动监听，注意是.add...以前是setOnPageChangeListener方法，已过时
        mViewPager.addOnPageChangeListener(this);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    //滑动后的监听
    @Override
    public void onPageSelected(int position) {
        //循环设置当前页的标记图
        for (int i = 0; i < mImageViewArray.length; i++) {
            mImageViewArray[position].setBackgroundResource(R.mipmap.ic_launcher);
            if (position != i) {
                mImageViewArray[i].setBackgroundResource(R.mipmap.checkbox_checked);
            }
        }
        //判断是否最后一页，是则显示button
//        if (position == mImageViewArray.length - 1) {
//            mButton.setVisibility(View.VISIBLE);
//        } else {
//            mButton.setVisibility(View.GONE);
//        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

}
